let {movieModel} = require("../Model");

let movieMock = require("../mock/movieMock.js");

// 往数据库加电影
async function addMovies(){
    let data=await movieModel.insertMany(movieMock);
    console.log(data);
}
//addMovies()

// 分页显示电影
async function getMovies(filter, page = 1, limit = 10) {
    let data = await movieModel.find(filter).populate("mateFilm").skip((page - 1) * limit).limit(limit);

    return data;

}

async function movieCount(filter) {
    let data = await movieModel.find(filter).count();
    return data;
}

// 新增电影
async function addMovie(movie) {
    let data = await movieModel.create(movie);
    return data;

}

// 通过id删除电影
async function deleteMovie(_id) {
    let data = await movieModel.deleteOne({_id});
    return data;
}

// 通过id更新电影
async function updateMovie(movie) {
    if (! movie || ! movie._id) 
        return null;
    
    let data = await movieModel.updateMany({
        _id: movie._id
    }, movie,)
    return data;
}

// 获取热播数据
async function getHots(filter, page = 1, limit = 10, sort = "_id") {
    let data = await movieModel.find({
        "ishot":1
    }).find(filter)
    
    .skip((page - 1) * limit)
    .limit(limit)
    .sort(sort);

    return data;
}

async function hotsCount(filter){
    let data=await movieModel.find(
        {"ishot":1}).find(filter).count;
        return data;
}

// getHots(null, 1, 10).then(d => {
//     console.log(d);
// })

// 获取热映数据

async function getHotsUp(filter, page = 1, limit = 10, sort = "_id") {
    let data = await movieModel.find({
        "ishot":2
    }).find(filter)
    
    .skip((page - 1) * limit)
    .limit(limit)
    .sort(sort);

    return data;
}

async function hotsUpCount(filter){
    let data=await movieModel.find(
        {"ishot":2}).find(filter).count;
        return data;
}

// getHotsUp(null, 1, 10).then(d => {
//     console.log(d);
// })



module.exports.hotsCount=hotsCount;
module.exports.hotsUpCount=hotsUpCount;
module.exports.getHots = getHots;
module.exports.getHotsUp = getHotsUp;
module.exports.getMovies = getMovies;
module.exports.movieCount = movieCount;
module.exports.addMovie = addMovie;
module.exports.deleteMovie = deleteMovie;
module.exports.updateMovie = updateMovie;
